#include<stdio.h>
#include<stdlib.h>
//to find the maximum and minimum element
void max_min_array(int *a,int n)
{
int i,max,min;
max=a[0];
min=a[0];
for(i=0;i<n;i++)
{
if(a[i]>=max)
max=a[i];
if(a[i]<=min)
min=a[i];
}
printf("max is%d", max);
printf("min is %d",min);
}
//to append element at the end of array
void append(int *a,int n, int d)
{

int i;
n=n+1;

a=(int *)realloc((int *)a,n*sizeof(int));
a[n-1]=d;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}}
//to delete an element
int del_array(int *a,int n, int d)
{
int i,j=0,f=0;
for(i=0;i<n;i++)
{
if(a[i]==d)
{f=1;
for(j=i;j<n-1;j++)
a[j]=a[j+1];
n--;
}}

a=(int *)realloc((int *)a,n*sizeof(int));


for(i=0;i<n;i++)
printf("%d",a[i]);
return f;
}
//to reverse the array
void reverse_array(int *a,int n)
{
int i,j,t;

for(i=0,j=n-1;i<n/2,j>=n/2;i++,j--)
{

t=a[i];
a[i]=a[j];
a[j]=t;
}
for(i=0;i<n;i++)
printf("%d",a[i]);
}
//to find the index of an element
void index_num(int *a,int n, int num)
{
int i,f=0;
for(i=0;i<n;i++)
{
if(a[i]==num)
{
printf("index is %d", i);
f=1;
break;}}
if(f==0)
printf("no.not found");
}
//to count the no. of occurences of an element
void count_num(int *a,int n,int x)
{
int i,c=0;
for(i=0;i<n;i++)
{if(a[i]==x)
c++;
}
printf("%d",c);
}
//to sort the given array
void sort_array(int *a, int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;}
}}
for(i=0;i<n;i++)
printf("%d",a[i]);
}
//to extend the array by appending another array to its end
void extend(int *a, int *a1,int n,int n1)
{

int i,j;
int n2=n;
n=n+n1;

a=(int *)realloc((int *)a,n*sizeof(int));
for(i=n2,j=0;i<n,j<n1;i++,j++)
a[i]=a1[j];

for(i=0;i<n;i++)
printf("%d", a[i]);
}
//to insert an element in a position
void insert(int *a,int n, int x, int d)
{
int i,j;
n=n+1;
a=(int *)realloc((int *)a,n*sizeof(int));
if(d>=n || d<0)
printf("index doesnt exist: ");
else
{
for(i=0;i<n;i++)
{
if(i==(d-1))
{
for(j=n-1;j>i;j--)
{
a[j]=a[j-1];
}
a[i]=x;
}}
}
for(i=0;i<n;i++)
{
printf("%d", a[i]);
}
}
//to pop an element of a position and return it
int pop_element(int *a, int n, int d)
{
int i,j,x;
if(d>=n || d<0)
printf("position doesnot exist: ");
else
{
for(i=0;i<n;i++)
{
if(i==(d-1))
{
x=a[i];
for(j=i;j<n-1;j++)

a[j]=a[j+1];

n--;
break;
}}}
for(i=0;i<n;i++)
printf("%d", a[i]);

return x;
}
//main
void main()
{
int n,n1,i,d,num,x,c,choice=1,f;
int *a, *a1;
printf("enter size of array:");

scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));

printf("enter elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
while(choice ==1)
{
printf("ENTER CHOICE: 1.max min 2.delete 3.reverse 4.search 5.count 6.sort 7.append 8.insert 9.extend 10.pop ");
scanf("%d", &c);
switch(c)
{
case 1:
max_min_array(a,n);
break;
case 2:
printf("enter element to be deleted: ");
scanf("%d", &d);
f=del_array(a,n,d);
if(f==0)
printf("element doesnt exist: ");
else
n--;
break;
case 3:
reverse_array(a,n);
break;
case 4:
printf("enter no.to search for: ");
scanf("%d",&num);
index_num(a,n,num);
break;
case 5:
printf("enter no.to count: ");
scanf("%d",&x);
count_num(a,n,x);
break;
case 6:
sort_array(a,n);
break;
case 7:
printf("enter element: ");
scanf("%d", &d);
append(a,n,d);
break;
case 8:
printf("enter element to insert: ");
scanf("%d", &x);
printf("enter position: ");
scanf("%d", &d);
insert(a,n,x,d);
break;
case 9:
printf("enter size of array:?");
scanf("%d", &n1);
a1=(int *)malloc(n1*sizeof(int));
printf("enter elements: ");
for(i=0;i<n1;i++)
scanf("%d",&a1[i]);
extend(a,a1,n,n1);
break;
case 10:
printf("enter position of element to be deleted: ");
scanf("%d", &d);
x=pop_element(a,n,d);
printf("deleted element %d",x);
break;
}
printf("do u want to continue?: yes:1 no: 0\n");
scanf("%d", &choice);

}
}



